package com.hzqy.service.statistics;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.product.ProductDao;
import com.hzqy.persistence.dao.statistics.OrderDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.SysProductVo;
import com.hzqy.web.vo.UserAccountVo;
import com.hzqy.web.vo.UserBillVo;

@Service
public class OrderServiceBean extends GenericServiceBean<PmsUserVo, OrderDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private OrderDao orderDao;
	@Resource
	private ProductDao productDao;

	@Override
	public OrderDao getEntityDao() {
		return orderDao;
	}
	public Map<String,Object> selectUserBillList(UserBillVo userBillVo, int curPage,
			int pageSize) {
		if(!"null".equals(userBillVo.getBeginDate()) &&!"".equals(userBillVo.getBeginDate()) ) {
			userBillVo.setBeginDate(userBillVo.getBeginDate()+" 00:00:00");
		}
		if(!"null".equals(userBillVo.getEndDate()) &&!"".equals(userBillVo.getEndDate()) ) {
			userBillVo.setEndDate(userBillVo.getEndDate()+" 23:59:59");
		}
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = orderDao.selectUserBillCount(userBillVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_ub_serverid", userBillVo.getF_ub_serverid());
			map.put("f_ub_productid", userBillVo.getF_ub_productid());
			map.put("f_ub_parentid", userBillVo.getF_ub_parentid());
			map.put("f_ub_productCode",userBillVo.getF_ub_productCode());
			map.put("f_ub_type", userBillVo.getF_ub_type());
			map.put("f_ui_resolution", userBillVo.getF_ui_resolution());
			map.put("beginDate", userBillVo.getBeginDate());
			map.put("endDate", userBillVo.getEndDate());
			map.put("f_ub_source", userBillVo.getF_ub_source());
			List<UserBillVo> results = orderDao.selectUserBillList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<UserBillVo>());
		}
		return map;
	}
	public Map<String,Object> selectExcelList(UserBillVo userBillVo) {
		if(!"null".equals(userBillVo.getBeginDate()) &&!"".equals(userBillVo.getBeginDate()) ) {
			userBillVo.setBeginDate(userBillVo.getBeginDate()+" 00:00:00");
		}
		if(!"null".equals(userBillVo.getEndDate()) &&!"".equals(userBillVo.getEndDate())) {
			userBillVo.setEndDate(userBillVo.getEndDate()+" 23:59:59");
		}
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = orderDao.selectExcelCount(userBillVo);
		System.out.println("excel = "+rowCount);
		map.put("total", rowCount);
		if(rowCount > 0) {
			map.put("f_ub_serverid", userBillVo.getF_ub_serverid());
			map.put("f_ub_productid", userBillVo.getF_ub_productid());
			map.put("f_ub_parentid", userBillVo.getF_ub_parentid());
			map.put("f_ub_productCode",userBillVo.getF_ub_productCode());
			map.put("f_ub_type", userBillVo.getF_ub_type());
			map.put("f_ui_resolution", userBillVo.getF_ui_resolution());
			map.put("beginDate", userBillVo.getBeginDate());
			map.put("endDate", userBillVo.getEndDate());
			map.put("f_ub_source", userBillVo.getF_ub_source());
			List<UserBillVo> results = orderDao.selectExcelList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<UserBillVo>());
		}
		return map;
	}
	
	public int unsubscribeUserBill(UserBillVo userBillVo){
		int flag = orderDao.unsubscribeUserBill(userBillVo);
		return flag;
	}
	
	public int insertUserBillZxbh(UserBillVo userBillVo){
		int flag = 0;
		String[] productidsArr = userBillVo.getProductids().split(",");
		String[] accountIdsArr = userBillVo.getAccountIds().split(",");
		String[] serveridsArr = userBillVo.getServerids().split(",");
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for(int i=0;i<productidsArr.length;i++){
			SysProductVo sysProductVo = productDao.selectProduct(Integer.parseInt(productidsArr[i]));
			int f_ub_type = sysProductVo.getF_sp_type();
			Date date = new Date();
			String f_ub_ordertime = sdf.format(date);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			String f_ub_endtime = sdf.format(date);
			if(1==f_ub_type){//包月
				cal.add(Calendar.MONTH, 1);
			}else if(2==f_ub_type){//包天
				cal.add(Calendar.DATE, 1);
			}else if(5==f_ub_type){//包年
				cal.add(Calendar.MONTH, 12);
			}else if(6==f_ub_type){//包季
				cal.add(Calendar.MONTH, 3);
			}
			Date date2 = cal.getTime();
			f_ub_endtime = sdf.format(date2);
			BigDecimal f_ub_fee = sysProductVo.getF_sp_fee();
			for(int j=0;j<accountIdsArr.length;j++){
				UserBillVo ubv = new UserBillVo();
				ubv.setF_ub_accountId(Integer.parseInt(accountIdsArr[j]));
				ubv.setF_ub_serverid(serveridsArr[j]);
				ubv.setF_ub_info(sysProductVo.getF_sp_name());
				ubv.setF_ub_productid(sysProductVo.getF_sp_id());
				ubv.setF_ub_type(f_ub_type);
				ubv.setF_ub_fee(f_ub_fee.toString());
				ubv.setF_ub_source("赠送");
				ubv.setF_ub_status("1");
				ubv.setF_ub_ordertime(f_ub_ordertime);
				ubv.setF_ub_endtime(f_ub_endtime);
				ubv.setF_ub_unsubtime(f_ub_endtime);
				flag += orderDao.insertUserBill(ubv);
			}
		}
		return flag;
	}
}
